<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="120px">
      <el-form-item label="名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入名称"
          clearable
          size="small"
          style="width:250px;"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="广告位" label-width="70px">
        <el-select v-model="queryParams.adPlace" placeholder="请选择广告位" style="width:250px;">
          <el-option
            v-for="dict in adPlaceOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="一级销区名称" prop="rp02name">
        <el-input
          v-model="queryParams.rp02name"
          placeholder="请输入一级销区名称"
          clearable
          size="small"
          style="width:250px;"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="二级销区名称" prop="rp03name">
        <el-input
          v-model="queryParams.rp03name"
          placeholder="请输入二级销区名称"
          clearable
          size="small"
          style="width:250px;"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="星选馆" prop="zdId">
        <el-select v-model="queryParams.zdId" placeholder="请选择星选馆" clearable size="small">
          <el-option
            v-for="dict in zdIdOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="是否展示" prop="isShow">
        <el-select v-model="queryParams.isShow" placeholder="请选择是否" clearable size="small" style="width: 150px;">
          <el-option
            v-for="dict in isShowOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="员工可见" prop="isNeedHn">
        <el-select v-model="queryParams.isNeedHn" placeholder="请选择是否" clearable size="small" style="width: 150px;">
          <el-option
            v-for="dict in isNeedHnOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" style="margin-left: 38px;" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" style="margin-left: 25px;" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['advent:advent:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['advent:advent:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['advent:advent:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['advent:advent:export']"
        >导出</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" border stripe :data="adventList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="名称" align="center" prop="name" />
      <el-table-column label="广告位" align="center" prop="adPlace" :formatter="adPlaceFormat" />
      <el-table-column label="图片" align="center" prop="picUrl">
        <template slot-scope="scope">
          <img v-if="scope.row.picUrl" :src="scope.row.picUrl" alt="" width="100%" height="100%">
        </template>
      </el-table-column>
      <el-table-column label="跳转url" align="center" prop="toUrl" />
      <el-table-column label="小程序跳转url" align="center" prop="toAppUrl" />

      <el-table-column label="星选馆" align="center" prop="zdId" :formatter="zdIdFormat" />
      <el-table-column label="排序" align="center" prop="orderId" width="70" />
      <el-table-column label="是否展示" align="center" prop="isShow" width="80" :formatter="isShowFormat" />
      <el-table-column label="是否有效" align="center" prop="isActive" width="80" :formatter="isActiveFormat" />
      <el-table-column label="员工可见" align="center" prop="isNeedHn" width="80" :formatter="isNeedHnFormat" />
      <!-- <el-table-column label="备注" align="center" prop="remark" /> -->
      <el-table-column label="操作" fixed="right" align="center" class-name="small-padding fixed-width" width="120">
        <template slot-scope="scope">
          <el-button v-hasPermi="['advent:advent:edit']" plain type="primary" size="small" @click="handleUpdate(scope.row)">编辑</el-button>
          <br v-hasPermi="['advent:advent:remove']">
          <br v-hasPermi="['advent:advent:remove']">
          <el-button v-hasPermi="['advent:advent:remove']" plain type="danger" size="small" @click="handleDelete(scope.row)">删除</el-button>
          <!-- <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['advent:advent:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['advent:advent:remove']"
          >删除</el-button> -->
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改广告位维护对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="90%" append-to-body center :close-on-click-modal="false" :close-on-press-escape="false">
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="名称" prop="name">
              <el-input v-model="form.name" placeholder="请输入名称" />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="排序" prop="orderId" label-width="55px">
              <el-input v-model="form.orderId" placeholder="请输入排序" />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="价格" prop="remark" label-width="55px">
              <el-input v-model="form.remark" placeholder="请输入价格" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="新闻图片" prop="picUrl">
              <el-upload :action="imageUploader.action" :headers="imageUploader.headers" :on-success="setFormImageUrl" list-type="picture-card" :file-list="imageUploader.fileList">
                <i slot="default" class="el-icon-plus"></i>
                <div slot="file" slot-scope="{file}">
                  <img class="el-upload-list__item-thumbnail" :src="file.url" alt="">
                  <span class="el-upload-list__item-actions">
                <span class="el-upload-list__item-preview" @click="setFormImagePreview(file)">
                  <i class="el-icon-zoom-in"></i>
                </span>
                <span class="el-upload-list__item-delete" @click="removeFormImageUrl(file)">
                  <i class="el-icon-delete"></i>
                </span>
              </span>
                </div>
              </el-upload>
              <el-dialog :visible.sync="imageUploader.isShowPreview">
                <img width="100%" :src="imageUploader.previewUrl" alt="">
              </el-dialog>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="跳转url" prop="toUrl">
              <el-input v-model="form.toUrl" placeholder="请输入跳转url" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="小程序id" prop="appid">
              <el-input v-model="form.appid" placeholder="请输入小程序id" />
            </el-form-item>
          </el-col>
          <el-col :span="16">
            <el-form-item label="小程序跳转url" prop="toAppUrl">
              <el-input v-model="form.toAppUrl" placeholder="请输入小程序跳转url" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="6">
            <el-form-item label="是否有效">
              <el-select v-model="form.isActive" placeholder="请选择是否有效">
                <el-option
                  v-for="dict in isActiveOptions"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="dict.dictValue"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="是否展示">
              <el-select v-model="form.isShow" placeholder="请选择是否展示">
                <el-option
                  v-for="dict in isShowOptions"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="dict.dictValue"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="员工可见" prop="isNeedHn">
              <el-select v-model="form.isNeedHn" placeholder="是否仅员工可见" clearable size="small">
                <el-option
                  v-for="dict in isNeedHnOptions"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="dict.dictValue"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="星选馆" prop="zdId">
              <el-select v-model="form.zdId" placeholder="请选择星选馆" clearable size="small">
                <el-option
                  v-for="dict in zdIdOptions"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="dict.dictValue"
                />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="6">
            <el-form-item label="一级销区" prop="rp02">
              <el-input v-model="form.rp02" placeholder="请输入一级销区" />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="一级销区名称" prop="rp02name">
              <el-input v-model="form.rp02name" placeholder="请输入一级销区名称" />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="二级销区" prop="rp03">
              <el-input v-model="form.rp03" placeholder="请输入二级销区" />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="二级销区名称" prop="rp03name">
              <el-input v-model="form.rp03name" placeholder="请输入二级销区名称" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="广告位置">
              <el-select v-model="form.adPlace" placeholder="请选择广告位置">
                <el-option
                  v-for="dict in adPlaceOptions"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="dict.dictValue"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="广告占比" prop="adPercent">
              <el-input v-model="form.adPercent" placeholder="请输入广告占比" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="广告内容" prop="adContent">
              <el-input v-model="form.adContent" placeholder="请输入广告内容" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listAdvent, getAdvent, delAdvent, addAdvent, updateAdvent, exportAdvent } from "@/api/advent/advent";

export default {
  name: "Advent",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 广告位维护表格数据
      adventList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 是否有效字典
      isActiveOptions: [],
      // 是否展示字典
      isShowOptions: [],
      // 广告位置字典
      adPlaceOptions: [],
      // 是否仅员工可见字典
      isNeedHnOptions: [],
      // 总店id
      zdIdOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: undefined,
        isShow: undefined,
        rp02name: undefined,
        rp03name: undefined,
        isNeedHn: undefined,
        adPlace: undefined,
        zdId: undefined
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      imageUploader:{
        action: process.env.VUE_APP_BASE_API+"/common/upload/news",
        headers: {
          Authorization: 'Bearer ' + this.$store.state.user.token,
          lesseeId: this.$store.state.user.lesseeid
        },
        fileList:[],
        isShowPreview:false,
        previewUrl:""
      }
    };
  },
  created() {
    this.getList();
    this.getDicts("is_active").then(response => {
      this.isActiveOptions = response.data;
    });
    this.getDicts("sys_yes_no").then(response => {
      this.isShowOptions = response.data;
      if (this.isShowOptions.length>0) {
        this.isShowOptions.forEach(function(item) {
          if (item.dictValue == 'Y') {
            item.dictValue = 1;
          } else {
            item.dictValue = 0;
          }
        })
        console.log('this.isShowOptions:')
        console.log(this.isShowOptions)
      }
    });
    this.getDicts("HN_ADVERT_PLACE").then(response => {
      this.adPlaceOptions = response.data;
    });
    this.getDicts("sys_yes_no").then(response => {
      this.isNeedHnOptions = response.data;
      if (this.isNeedHnOptions.length>0) {
        this.isNeedHnOptions.forEach(function(item) {
          if (item.dictValue == 'Y') {
            item.dictValue = 1;
          } else {
            item.dictValue = 0;
          }
        })
        console.log('this.isNeedHnOptions:')
        console.log(this.isNeedHnOptions)
      }
    });
    this.getDicts("HN_STAR_DESIGN").then(response => {
      this.zdIdOptions = response.data;
    });
  },
  methods: {
    gitZD(ZDId) {
      let ZDName = '';
      const that = this;
      for (let i in that.zdIdOptions) {
        if (that.zdIdOptions[i].dictValue == ZDId) {
          ZDName = that.zdIdOptions[i].dictLabel;
          break;
        }
      }
      return ZDName;
    },
    gitADPlace(adPlace) {
      let ADPlaceName = '';
      const that = this;
      for (let i in that.adPlaceOptions) {
        if (that.adPlaceOptions[i].dictValue == adPlace) {
          ADPlaceName = that.adPlaceOptions[i].dictLabel;
          break;
        }
      }
      return ADPlaceName;
    },
    setFormImageUrl(response, file, fileList){
      this.imageUploader.fileList = [file]
      this.form.picUrl = response.url
    },
    setFormImagePreview(file){
      this.imageUploader.isShowPreview = !this.imageUploader.isShowPreview
      this.imageUploader.previewUrl = file.url
    },
    removeFormImageUrl(file){
      this.imageUploader.previewUrl = ""
      let index = this.imageUploader.fileList.findIndex(obj => obj == file)
      if(index>-1){
        this.imageUploader.fileList.splice(index,1)
      }
    },
    /** 查询广告位维护列表 */
    getList() {
      this.loading = true;
      listAdvent(this.queryParams).then(response => {
        this.adventList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 总店ID字典翻译
    zdIdFormat(row, column) {
      return this.selectDictLabel(this.zdIdOptions, row.zdId);
    },
    // 是否有效字典翻译
    isActiveFormat(row, column) {
      return this.selectDictLabel(this.isActiveOptions, row.isActive);
    },
    // 是否展示字典翻译
    isShowFormat(row, column) {
      return this.selectDictLabel(this.isShowOptions, row.isShow);
    },
    // 广告位置字典翻译
    adPlaceFormat(row, column) {
      return this.selectDictLabel(this.adPlaceOptions, row.adPlace);
    },
    // 是否仅员工可见字典翻译
    isNeedHnFormat(row, column) {
      return this.selectDictLabel(this.isNeedHnOptions, row.isNeedHn);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        createBy: undefined,
        createTime: undefined,
        updateTime: undefined,
        updateBy: undefined,
        isActive: undefined,
        lesseeId: undefined,
        name: undefined,
        picUrl: undefined,
        toUrl: undefined,
        toAppUrl: undefined,
        isShow: undefined,
        orderId: undefined,
        rp02: undefined,
        rp02name: undefined,
        rp03: undefined,
        rp03name: undefined,
        zdId: undefined,
        adPlace: undefined,
        adContent: undefined,
        adPercent: undefined,
        appid: undefined,
        isNeedHn: undefined,
        remark: undefined
      };
      this.imageUploader.fileList = [];
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams.adPlace = '';
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!=1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.form.isActive = this.isActiveOptions[0].dictValue;
      this.form.isShow = this.isShowOptions[0].dictValue;
      this.form.isNeedHn = this.isNeedHnOptions[1].dictValue;
      this.form.zdId = this.zdIdOptions[0].dictValue;
      this.open = true;
      this.title = "添加广告位维护";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getAdvent(id).then(response => {
        this.form = response.data;
        if(this.form.picUrl){
          this.imageUploader.fileList = [{url:this.form.picUrl}]
        }
        this.open = true;
        this.title = "修改广告位维护";
      });
    },
    /** 提交按钮 */
    submitForm: function() {
      if (this.loading == true) {
        return;
      }
      this.loading = true;
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != undefined) {
            updateAdvent(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("修改成功");
                this.open = false;
                this.loading = false;
                this.getList();
              }
            });
          } else {
            addAdvent(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("新增成功");
                this.open = false;
                this.loading = false;
                this.getList();
              }
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除广告位维护编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delAdvent(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(function() {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有广告位维护数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportAdvent(queryParams);
        }).then(response => {
          this.download(response.msg);
        }).catch(function() {});
    }
  }
};
</script>
<style>
  .el-dialog__body{
    padding: 30px 30px 30px 0;
  }
  .el-dialog__header{
    width: 100%;
    text-align: center;
  }
  .el-dialog__body .el-select{
    width: 100%;
  }
  .el-dialog__footer{
    text-align: center;
  }
  .el-dialog__footer .el-button+.el-button{
    margin-left: 30px;
  }
  .el-upload-list--picture-card .el-upload-list__item>div{
    width: 100%;
    height: 100%;
    display: flex;
    justify-content: center;
    align-items: center;
  }
  .el-upload-list--picture-card .el-upload-list__item div img{
    display: inline-block;
    width: 100%;
    height: auto;
  }
</style>
